/*
Copyright (C) 2009  Raymond Jeon
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "gl_lib_include.h"
#include "gl_smmp_func.h"
#include "gl_smmp_funcbasicconf.h"
#include "gl_smmp_funcmonitor.h"
#include "gl_smmp_funcspam.h"
#include "gl_smmp_funcuser.h"
#include "gl_smmp_response.h"
#include "gl_mail_define.h"
#include "gl_mail_log.h"
#include "gl_mail_usercheck.h"
#include "gl_mail_user.h"
#include "gl_mail_config.h"
#include "gl_mail_statistics.h"


////////////////////////////////////////////////////////////////////////////////
//	Date : 2003-3-25
//	Update : 
//	Author : Raymond Jeon
//	Parameters
//	Return Value  
//	Remarks 
////////////////////////////////////////////////////////////////////////////////

void GlSmmpFuncUser(glsmmpsession_ *gsn, char *szVal)
{
	GlStrWordCpy(gsn->szUser, szVal, 256);
	GlSocketStreamPrintfLnLog(gsn->stream, GlLogSmmp, SMMP_OK_USERNAME);
}






////////////////////////////////////////////////////////////////////////////////
//	Date : 2003-3-25
//	Update : 
//	Author : Raymond Jeon
//	Parameters
//	Return Value  
//	Remarks 
////////////////////////////////////////////////////////////////////////////////

void GlSmmpFuncPass(glsmmpsession_ *gsn, char *szVal)
{
	if (!__VAL(gsn->szUser))
	{
		GlSocketStreamPrintfLnLog(gsn->stream, GlLogSmmp, SMMP_ERR_USER_NEED);
		return;
	}
	GlStrWordCpy(gsn->szPass, szVal, 256);

	if (GlUserEmailorIdGetPwDir(gsn->szUser, gsn->szPass, gsn->szDir))
	{
		GlSprintf(gsn->szConfFile, _MAXPATH, "%s\\%s\\%s", gsn->szDir, "cf", "mail.cf");
		gsn->iStat = SMMP_USER;
		GlSocketStreamPrintfLnLog(gsn->stream, GlLogSmmp, SMMP_OK_USERREADY);
	}
	else
	{
		GlSocketStreamPrintfLnLog(gsn->stream, GlLogSmmp, SMMP_ERR_USER_REFUSED, gsn->szUser);
	}
}



void GlSmmpFuncAdmin(glsmmpsession_ *gsn, char *szVal)
{
	GlStrWordCpy(gsn->szPass, szVal, 256);

	if (!strcmp(gsn->szPass, __SZ(glData.szAdminPw)))
	{
		GlStrSizeCpy(gsn->szDir, glData.szDirCurWork, _MAXPATH);
		GlSprintf(gsn->szConfFile, _MAXPATH, "%s\\%s\\%s", gsn->szDir, "cf", "mail.cf");
		gsn->iStat = SMMP_ADMIN;
		GlSocketStreamPrintfLnLog(gsn->stream, GlLogSmmp, SMMP_OK_ADMINREADY);
	}
	else
	{
		GlSocketStreamPrintfLnLog(gsn->stream, GlLogSmmp, SMMP_ERR_ADMIN_WRONGPASS);
	}
}





////////////////////////////////////////////////////////////////////////////////
//	Date : 2003-3-25
//	Update : 
//	Author : Raymond Jeon
//	Parameters
//	Return Value  
//	Remarks 
////////////////////////////////////////////////////////////////////////////////

void GlSmmpFuncNoop(glsmmpsession_ *gsn, char *szVal)
{
	GlSocketStreamPrintfLnLog(gsn->stream, GlLogSmmp, "+OK Noop");
}







////////////////////////////////////////////////////////////////////////////////
//	Date : 2003-3-25
//	Update : 
//	Author : Raymond Jeon
//	Parameters
//	Return Value  
//	Remarks 
////////////////////////////////////////////////////////////////////////////////

void GlSmmpFuncHelp(glsmmpsession_ *gsn, char *szVal)
{
	glcommand_ *cmd;

	if (!__VAL(szVal))
	{
		switch (gsn->iStat)
		{
		case SMMP_AUTH :
            GlSocketStreamPrintStrLnLog(gsn->stream, GlLogSmmp, SMMP_RES_AUTHHELP);
			return;
		case SMMP_USER :
			GlSocketStreamPrintStrLnLog(gsn->stream, GlLogSmmp, SMMP_RES_USERHELP);
			return;
		case SMMP_ADMIN :
			GlSocketStreamPrintStrLnLog(gsn->stream, GlLogSmmp, SMMP_RES_ADMINHELP);
			return;
		default :
			GlSocketStreamPrintStrLnLog(gsn->stream, GlLogSmmp, SMMP_ERR_UNREC_COMMAND);
			return;
		}
	}

	cmd = GlGetCommandName(szVal, GlSmmpCommand);
	
	if (cmd)
		GlSocketStreamPrintStrLnLog(gsn->stream, GlLogSmmp, cmd->pHelp);
	else
		GlSocketStreamPrintStrLnLog(gsn->stream, GlLogSmmp, SMMP_ERR_HELPUNKNOWN);
}





////////////////////////////////////////////////////////////////////////////////
//	Date : 2003-3-25
//	Update : 
//	Author : Raymond Jeon
//	Parameters
//	Return Value  
//	Remarks 
////////////////////////////////////////////////////////////////////////////////

void GlSmmpFuncQuit(glsmmpsession_ *gsn, char *szVal)
{
	GlSocketStreamPrintStrLnLog(gsn->stream, GlLogSmmp, SMMP_RES_QUIT);

	gsn->bQuit = TRUE;
	gsn->stream->bClose = TRUE;
}
    


